package com.b.server.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Project: kim-sleuth-zipkin
 * @PackageName: com.b.server.controller
 * @FileName: BserverController.java
 * @Description: 限流测试类 QPS
 * @Author: kimwu
 * @Time: 2021-01-04 17:04:21
 */
@RestController
@RequestMapping("/b")
@Slf4j
public class FlowControlController {

    int i = 1;

    /**
     * 测试sentinel 流控规则
     *      首页 -> 服务名 -> 流控规则
     * @return
     */
    @PostMapping("/flowControl")
    //定义资源 value:设置资源的名称 blockHandler:设置限流或降级的处理函数
    @SentinelResource(value = "flowControl", blockHandler = "exceptionHandler")
    public String flowControl() {
        log.info("b服务正在被调用 {} ", i++);
        return "ok";
    }

    //被限流的处理函数
    public String exceptionHandler(BlockException ex) {
        ex.printStackTrace();
        return "qps限制，请稍候再试";
    }

    @PostMapping("/noMean")
    public String noMean(){
        log.info("b服务正在被调用，没有任何注解");
        return "ok";
    }
}
